﻿#region Using statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Rectangular.Model.Entities;
using Rectangular.Model.Environment;
using Rectangular.Util;
#endregion

namespace Rectangular.Controller
{
    public static class CollisionManager
    {
        private static Random _random = new Random();

        /// <summary>
        /// Handles collisions between bullets and monsters
        /// </summary>
        /// <param name="bullets"></param>
        /// <param name="monsters"></param>
        public static void ProcessBulletsVsMonsters(List<Bullet> bullets, List<Monster> monsters)
        {
            for (int i = 0; i < bullets.Count; i++)
            {
                for (int j = 0; j < monsters.Count; j++)
                {
                    if (bullets[i].GetBoundingBox().Intersects(monsters[j].GetBoundingBox()))
                    {
                        monsters[j].Health -= 30;
                        bullets.Remove(bullets[i]);

                        if (monsters[j].Health < 0)
                        {
                            monsters[j].Position = new Vector2(-_random.Next(50), -_random.Next(50));
                            monsters[j].Destination = VectorHelper.GenerateVector(800, 800);
                            monsters[j].Health = 200;
                        }

                        break;
                    }
                }
            }
        }

        /// <summary>
        /// Handles collision between the player and the environment
        /// </summary>
        /// <param name="player"></param>
        /// <param name="map"></param>
        public static void ProcessPlayerVsEnvironmentHorizontal(Player player, TileMap map)
        {
            foreach (Tile tile in map.Tiles)
            {
                if (tile != null)
                {
                    if (player.GetBoundingBox().Intersects(tile.GetBoundingBox()))
                    {
                        if (player.Position.X < tile.Position.X)
                        {
                            player.Position = new Vector2(tile.Position.X - 1 - player.Width, player.Position.Y);
                        }
                        else
                        {
                            player.Position = new Vector2(tile.Position.X + tile.Width + 1, player.Position.Y);
                        }
                    }
                }
            }
        }

        public static void ProcessPlayerVsEnvironmentVertical(Player player, TileMap map)
        {
            foreach (Tile tile in map.Tiles)
            {
                if (tile != null)
                {
                    if (player.GetBoundingBox().Intersects(tile.GetBoundingBox()))
                    {
                        if (player.Position.Y < tile.Position.Y)
                        {
                            player.Position = new Vector2(player.Position.X, tile.Position.Y - 1 - player.Height);
                        }
                        else
                        {
                            player.Position = new Vector2(player.Position.X, tile.Position.Y + tile.Height + 1);
                        }
                    }
                }
            }
        }
    }
}